﻿using System;
using System.Drawing;
using System.Runtime.InteropServices;
using pub.Interop;

namespace pub.Functions
{
    public class HackFuncs
    {
        private static int CalculateAbsoluteCoordinateX(int x)
        {
            return (x*65536)/Win32.GetSystemMetrics(Win32.SystemMetric.SM_CXSCREEN);
        }

        private static int CalculateAbsoluteCoordinateY(int y)
        {
            return (y*65536)/Win32.GetSystemMetrics(Win32.SystemMetric.SM_CYSCREEN);
        }

        public static void SimulateClick(IntPtr hWnd, int clientX, int clientY)
        {
            Win32.RECT rc;
            Win32.GetWindowRect(hWnd, out rc);
            if (rc.left < 0)
            {
                return;
            }
            var x = CalculateAbsoluteCoordinateX(rc.left + 5);
            var y = CalculateAbsoluteCoordinateY(rc.top + 5);

            Point pt;
            Win32.GetCursorPos(out pt);

            var inputs = new[] {
                    new Win32.INPUT {
                            type = 0,
                            U = new Win32.InputUnion {
                                    mi = new Win32.MOUSEINPUT {
                                            dx = x,
                                            dy = y,
                                            mouseData = Win32.MouseEventDataXButtons.Nothing,
                                            dwFlags =
                                                    Win32.MOUSEEVENTF.ABSOLUTE |
                                                    Win32.MOUSEEVENTF.MOVE,
                                            time = 0,
                                            dwExtraInfo = UIntPtr.Zero
                                    }
                            }
                    },
                    new Win32.INPUT {
                            type = 0,
                            U = new Win32.InputUnion {
                                    mi = new Win32.MOUSEINPUT {
                                            dx = x,
                                            dy = y,
                                            mouseData = Win32.MouseEventDataXButtons.Nothing,
                                            dwFlags = Win32.MOUSEEVENTF.LEFTDOWN,
                                            time = 0,
                                            dwExtraInfo = UIntPtr.Zero
                                    }
                            }
                    },
                    new Win32.INPUT {
                            type = 0,
                            U = new Win32.InputUnion {
                                    mi = new Win32.MOUSEINPUT {
                                            dx = x,
                                            dy = y,
                                            mouseData = Win32.MouseEventDataXButtons.Nothing,
                                            dwFlags = Win32.MOUSEEVENTF.LEFTUP,
                                            time = 0,
                                            dwExtraInfo = UIntPtr.Zero
                                    }
                            }
                    }
            };
            Win32.SendInput(3, inputs, Marshal.SizeOf(inputs[0]));
            Win32.SetCursorPos(pt.X, pt.Y);
        }
    }
}